clear *

use "${root}/data/processed/final_sample.dta", clear

drop if irregular == 1

********* Summary Statistics Public Finance Outcomes **********

foreach outcome in margin_mayor_left tot_exp_avg_pc tot_exp_avg_sgdp tot_rev_avg_pc tot_rev_avg_sgdp current_exp_avg_share investments_avg_share personnel_avg_share social_exp_avg_share health_avg_share education_avg_share welfare_avg_share nonsocial_exp_avg_share housing_avg_share transport_avg_share other_avg_share {

*** Summary Baseline
	sum `outcome' if baseline_sample == 1
	local min_`outcome'_1 : di %9.2fc `r(min)'
	local max_`outcome'_1 : di %9.2fc `r(max)'
	local mean_`outcome'_1 : di %9.2fc `r(mean)'
	local sd_`outcome'_1 : di %9.2fc `r(sd)'
	local N_`outcome'_1 : di %8.0g `r(N)'

*** Summary lame Duck
	sum `outcome' if baseline_sample == 1 & lame_duck == 1
	local min_`outcome'_2 : di %9.2fc `r(min)'
	local max_`outcome'_2 : di %9.2fc `r(max)'
	local mean_`outcome'_2 : di %9.2fc `r(mean)'
	local sd_`outcome'_2 : di %9.2fc `r(sd)'
	local N_`outcome'_2 : di %8.0g `r(N)'

*** Summary Tiebout median
	sum `outcome' if baseline_sample == 1 & tiebout_median_sample == 1
	local min_`outcome'_3 : di %9.2fc `r(min)'
	local max_`outcome'_3 : di %9.2fc `r(max)'
	local mean_`outcome'_3 : di %9.2fc `r(mean)'
	local sd_`outcome'_3 : di %9.2fc `r(sd)'
	local N_`outcome'_3 : di %8.0g `r(N)'

*** Summary Tiebout 75th
	sum `outcome' if baseline_sample == 1 & coal_dist_median_sample == 1
	local min_`outcome'_4 : di %9.2fc `r(min)'
	local max_`outcome'_4 : di %9.2fc `r(max)'
	local mean_`outcome'_4 : di %9.2fc `r(mean)'
	local sd_`outcome'_4 : di %9.2fc `r(sd)'
	local N_`outcome'_4 : di %8.0g `r(N)'

*** Summary Oil Windfall
	sum `outcome' if baseline_sample == 1 & oil_sample == 1
	local min_`outcome'_5 : di %9.2fc `r(min)'
	local max_`outcome'_5 : di %9.2fc `r(max)'
	local mean_`outcome'_5 : di %9.2fc `r(mean)'
	local sd_`outcome'_5 : di %9.2fc `r(sd)'
	local N_`outcome'_5 : di %8.0g `r(N)'

}


***** LATEX TABLE ****

* write table
texdoc init "${root}/results/tables/summary_stats.tex", replace force

tex \caption{Descriptive Statistics} 
tex \resizebox{\linewidth}{!}{
tex \begin{tabularx}{\linewidth}{l *5{>{\Centering}X}}
tex \toprule

tex 													&        Baseline 				& \multicolumn{4}{c}{Subsamples} 								\\
tex \cmidrule(lr){2-2} \cmidrule(lr){3-6}

tex 													& 								& 	Lame Duck 			& Tiebout $<$ median	 								& Ideology distance $>$ median  						& Oil windfall					\\
tex \midrule
tex \multicolumn{6}{l}{Size of government: overall revenues and expenses} \\
tex \midrule
tex Expenditure per capita		&		`mean_tot_exp_avg_pc_1'		  &		`mean_tot_exp_avg_pc_2'			& 	`mean_tot_exp_avg_pc_3'			&		`mean_tot_exp_avg_pc_4'			&	 	`mean_tot_exp_avg_pc_5'		 	\\
tex 													&		(`sd_tot_exp_avg_pc_1')  			&		(`sd_tot_exp_avg_pc_2')	 			&			(`sd_tot_exp_avg_pc_3') 		&		(`sd_tot_exp_avg_pc_4') 			&		(`sd_tot_exp_avg_pc_5') 			\\
tex Expenditure, \% GDP				&		`mean_tot_exp_avg_sgdp_1'   &		`mean_tot_exp_avg_sgdp_2'		&		`mean_tot_exp_avg_sgdp_3'		&		`mean_tot_exp_avg_sgdp_4'		&		`mean_tot_exp_avg_sgdp_5'	 	\\
tex 													&		(`sd_tot_exp_avg_sgdp_1')  		&		(`sd_tot_exp_avg_sgdp_2')	 		&			(`sd_tot_exp_avg_sgdp_3') 	&		(`sd_tot_exp_avg_sgdp_4') 		&		(`sd_tot_exp_avg_sgdp_5') 		\\
tex Revenue per capita				&		`mean_tot_rev_avg_pc_1'		  &		`mean_tot_rev_avg_pc_2'			&		`mean_tot_rev_avg_pc_3'			&		`mean_tot_rev_avg_pc_4'			& 	`mean_tot_rev_avg_pc_5'			\\
tex 													&		(`sd_tot_rev_avg_pc_1')  			&		(`sd_tot_rev_avg_pc_2')	 			&			(`sd_tot_rev_avg_pc_3') 		&		(`sd_tot_rev_avg_pc_4') 			&		(`sd_tot_rev_avg_pc_5')				\\
tex Revenue, \% GDP						&		`mean_tot_rev_avg_sgdp_1'   &		`mean_tot_rev_avg_sgdp_2'		&		`mean_tot_rev_avg_sgdp_3'		&		`mean_tot_rev_avg_sgdp_4'		&		`mean_tot_rev_avg_sgdp_5'	 	\\
tex 													&		(`sd_tot_rev_avg_sgdp_1')  		&		(`sd_tot_rev_avg_sgdp_2')	 		&			(`sd_tot_rev_avg_sgdp_3') 	&		(`sd_tot_rev_avg_sgdp_4')			&		(`sd_tot_rev_avg_sgdp_5') 		\\
tex \midrule
tex \multicolumn{6}{l}{Allocation of resources: budget categories (\% of total expenditure)} \\
tex \midrule
tex Current Expenditure			&		`mean_current_exp_avg_share_1'		&		`mean_current_exp_avg_share_2'		&		`mean_current_exp_avg_share_3'			&		`mean_current_exp_avg_share_4'		&		`mean_current_exp_avg_share_5'			\\
tex 												&		(`sd_current_exp_avg_share_1')  		&		(`sd_current_exp_avg_share_2')	 		&		(`sd_current_exp_avg_share_3') 				&		(`sd_current_exp_avg_share_4') 			&		(`sd_current_exp_avg_share_5') 				\\
tex \multicolumn{6}{l}{of which:} \\
tex \hspace{0.20cm} Personnel								&		`mean_personnel_avg_share_1'			&		`mean_personnel_avg_share_2'			&		`mean_personnel_avg_share_3'				&		`mean_personnel_avg_share_4'			&		`mean_personnel_avg_share_5'				\\
tex 												&		(`sd_personnel_avg_share_1')  			&		(`sd_personnel_avg_share_2')				&		(`sd_personnel_avg_share_3') 					&		(`sd_personnel_avg_share_4') 				&		(`sd_personnel_avg_share_5') 					\\
tex Public Investments			&		`mean_investments_avg_share_1'		&		`mean_investments_avg_share_2'		&		`mean_investments_avg_share_3'			&		`mean_investments_avg_share_4'		&		`mean_investments_avg_share_5'			\\
tex 												&		(`sd_investments_avg_share_1')  		&		(`sd_investments_avg_share_2')	 		&		(`sd_investments_avg_share_3') 				&		(`sd_investments_avg_share_4') 			&		(`sd_investments_avg_share_5')				\\
tex \midrule
tex \multicolumn{6}{l}{Allocation of resources: functional categories (\% of total expenditure)} \\
tex \midrule
tex Social Expenditures			&		`mean_social_exp_avg_share_1'			&		`mean_social_exp_avg_share_2'			&		`mean_social_exp_avg_share_3'			&		`mean_social_exp_avg_share_4'			&		`mean_social_exp_avg_share_5'			\\
tex 												&		(`sd_social_exp_avg_share_1')			&		(`sd_social_exp_avg_share_2')			&		(`sd_social_exp_avg_share_3')			&		(`sd_social_exp_avg_share_4')			&		(`sd_social_exp_avg_share_5')			\\
tex \multicolumn{6}{l}{of which:} \\
tex \hspace{0.20cm} Health \& sanitation		&		`mean_health_avg_share_1'					&		`mean_health_avg_share_2'					&		`mean_health_avg_share_3'					&		`mean_health_avg_share_4'					&		`mean_health_avg_share_5'					\\
tex 												&		(`sd_health_avg_share_1')					&		(`sd_health_avg_share_2')					&		(`sd_health_avg_share_3')					&		(`sd_health_avg_share_4')					&		(`sd_health_avg_share_5')					\\
tex \hspace{0.20cm} Education \& culture 		&		`mean_education_avg_share_1'			& 	`mean_education_avg_share_2'			& 	`mean_education_avg_share_3'			& 	`mean_education_avg_share_4'			& 	`mean_education_avg_share_5'			\\
tex 										 		&		(`sd_education_avg_share_1')			& 	(`sd_education_avg_share_2')			& 	(`sd_education_avg_share_3')			& 	(`sd_education_avg_share_4')			& 	(`sd_education_avg_share_5')			\\
tex \hspace{0.20cm} Social welfare					&		`mean_welfare_avg_share_1'				&		`mean_welfare_avg_share_2'				&		`mean_welfare_avg_share_3'				&		`mean_welfare_avg_share_4'				&		`mean_welfare_avg_share_5'				\\
tex								 					&		(`sd_welfare_avg_share_1')				&		(`sd_welfare_avg_share_2')				&		(`sd_welfare_avg_share_3')				&		(`sd_welfare_avg_share_4')				&		(`sd_welfare_avg_share_5')				\\
tex \multicolumn{6}{l}{Other Expenditures:} \\
tex \hspace{0.20cm} Housing									&		`mean_housing_avg_share_1'				&		`mean_housing_avg_share_2'				&		`mean_housing_avg_share_3'				&		`mean_housing_avg_share_4'				&		`mean_housing_avg_share_5'				\\
tex 												&		(`sd_housing_avg_share_1')				&		(`sd_housing_avg_share_2')				&		(`sd_housing_avg_share_3')				&		(`sd_housing_avg_share_4')				&		(`sd_housing_avg_share_5')				\\
tex \hspace{0.20cm} Transportation					&		`mean_transport_avg_share_1'			& 	`mean_transport_avg_share_2'			& 	`mean_transport_avg_share_3'			& 	`mean_transport_avg_share_4'			& 	`mean_transport_avg_share_5'			\\
tex 												&		(`sd_transport_avg_share_1')			& 	(`sd_transport_avg_share_2')			& 	(`sd_transport_avg_share_3')			& 	(`sd_transport_avg_share_4')			& 	(`sd_transport_avg_share_5')			\\
tex \hspace{0.20cm} Other 									&		`mean_other_avg_share_1'					&		`mean_other_avg_share_2'					&		`mean_other_avg_share_3'					&		`mean_other_avg_share_4'					&		`mean_other_avg_share_5'					\\
tex		 		 									&		(`sd_other_avg_share_1')					&		(`sd_other_avg_share_2')							&		(`sd_other_avg_share_3')					&		(`sd_other_avg_share_4')					&		(`sd_other_avg_share_5')					\\
tex \midrule
tex Left candidate margin 									&		`mean_margin_mayor_left_1'					&		`mean_margin_mayor_left_2'					&		`mean_margin_mayor_left_3'					&		`mean_margin_mayor_left_4'					&		`mean_margin_mayor_left_5'					\\
tex		 		 									&		(`sd_margin_mayor_left_1')					&		(`sd_margin_mayor_left_2')							&		(`sd_margin_mayor_left_3')					&		(`sd_margin_mayor_left_4')					&		(`sd_margin_mayor_left_5')					\\
tex \midrule
tex Observations 									& `N_tot_exp_avg_pc_1' 				& `N_tot_exp_avg_pc_2' 				& `N_tot_exp_avg_pc_3'				 & `N_tot_exp_avg_pc_4'					 & `N_tot_exp_avg_pc_5'					 \\
tex \bottomrule
tex \end{tabularx}}

texdoc close
